<?php
namespace app\controller;

use app\BaseController;
use app\model\AudioFragment;

class AudioEnglish extends BaseController
{
    public function index()
    {
        return '';
    }

    public function add () {
        $audio_fragment = new AudioFragment;
        if(!isset($_POST['data']) || !isset($_POST['audioid'])) exit('error');
        $audioid = (int) $_POST['audioid'];
        $dataArr = json_decode($_POST['data'], true);
        $saveData = [];
        if($dataArr) {
            // 批量插入数据
            foreach($dataArr as $key => $data ) {
                $newdata = [
                    'audioid' => $audioid,
                    'order'   => $key,
                    'start'   => isset($data['start']) ? $data['start'] : 0,
                    'end'     => isset($data['end']) ? $data['end'] : 0,
                    'text'    => isset($data['text']) ? $data['text'] : 0,
                ];
                array_push($saveData, $newdata);
            }
            AudioFragment::destroy(function($query) use ($audioid) {
                $query->where('audioid', '=', $audioid);
            });
            $audio_fragment->saveAll($saveData);
        }
    }
}
